<?php 
	class advertisings extends CI_Model{
		protected $table='advertising';
        protected $cate_table='ad_position';
	 	function __construct(){
        	parent::__construct();
    	}

		function update($id,$data){
			$this->db->where('id', $id);
 			return $this->db->update($this->table, $data);
		}

		function update_cate($id,$data){
			$this->db->where('id', $id);
 			return $this->db->update($this->cate_table, $data);
		}

    	function get_list($limit = 0, $offset = 0, $data = 0){
			$where='';
			if($data['search']){
                $where .= " and ad_name LIKE '%".$data['search']."%'";
            }
            if($data['sign']){
                $where .= " and ad_position_sign = '".$data['sign']."'";
            }
    		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}".$this->table." where 1=1 $where ORDER BY sort,add_time desc ";
			if($offset){
				$sql .= " LIMIT ".$limit.",".$offset;
			}
    		$result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
    	}


    	function get_cate_list($limit = 0, $offset = 0, $data = 0){
			$where='';
			if($data['search']){
				$where .= " and ad_position_name LIKE '%".$data['search']."%'";
			}
    		$sql = "SELECT * FROM {$this->db->dbprefix}".$this->cate_table." where 1=1 $where ORDER BY sort,add_time desc ";
			if($offset){
				$sql .= " LIMIT ".$limit.",".$offset;
			}
    		$result = $this->db->query($sql)->result_array();
			if($data['search']){
				foreach ($result as $key => $value) {
					$result[$key]['ad_position_name'] = str_replace($data['search'],'<font color="red">'.$data['search'].'</font>',$value['name']);
				}
			}
    		return $result;
    	}


    	function get_cat_list($id = 0){
    		$sql = "SELECT * FROM {$this->db->dbprefix}".$this->cate_table." where 1=1 $where ORDER BY sort,add_time desc ";
    		$result = $this->db->query($sql)->result_array();
    		return $result;
    	}

    	function get_cate_count($data){
			$where='';
			if($data['search']){
				$where .= " and ad_position_name LIKE '%".$data['search']."%'";
			}
    		$sql = "SELECT count(id) AS count FROM {$this->db->dbprefix}".$this->cate_table." where 1=1 $where";
    		$result = $this->db->query($sql)->row_array();
    		return $result['count'];
    	}

        function batch_delete($ids){
            $ids = implode(",",$ids);
            $sql = "DELETE FROM {$this->db->dbprefix}".$this->table." WHERE id in($ids)";
            $this->db->query($sql);
        }

        function batch_cate_delete($ids){
            $ids = implode(",",$ids);
            $sql = "DELETE FROM {$this->db->dbprefix}".$this->cate_table." WHERE id in($ids)";
            $this->db->query($sql);
        }

        function get_cate_id($pid = 0){
        	$sql = "SELECT id,name as cat_name from {$this->db->dbprefix}".$this->cate_table." where pid = ".$pid." ";
        	$result = $this->db->query($sql)->result_array();
        	return $result;
        }

        function get_ad_cate_info($ids){
             $ids = implode(",",$ids);
             $sql = "SELECT id, project_type, sign FROM {$this->db->dbprefix}ad_position WHERE id in($ids)";
             $result = $this->db->query($sql)->result_array();
             return $result;
        }

        function get_ad_by_sign($sign){
            $sql = "SELECT * FROM {$this->db->dbprefix}" . $this->table . " WHERE ad_position_sign = '{$sign}' AND status = 1";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }
    }